package com.ipay.wechat.factory;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.ipay.wechat.component.Wechat;

@Component
public class AuthorizeFactory {
    private Logger logger = LoggerFactory.getLogger(getClass());
    private String redirect_uri = null;
    private String scope = null;
    @Autowired
    private Wechat wechat;

    public String dealAuthorize(String action, String params) {
        // TODO Auto-generated method stub
        switch (action) {
            case "apply":
                redirect_uri = wechat.getBaseUrl() + "auth/isApply";
                scope = "snsapi_userinfo";
                break;
            case "center":
                redirect_uri = wechat.getBaseUrl() + "auth/isMerchant";
                scope = "snsapi_userinfo";
                break;
            case "tradingCenter": ///推送信息 链接 进入处理【订单列表】
                redirect_uri = wechat.getBaseUrl() + "trading/center";
                scope = "snsapi_userinfo";
                break;
            case "userAgent":
                redirect_uri = wechat.getBaseUrl() + "auth/userAgent" + params;// 微信支付进入
                scope = "snsapi_base";
                break;
            case "payGateway":
                redirect_uri = wechat.getBaseUrl() + "auth/payGateway" + params;// 第三方发起支付 微信支付进入
                scope = "snsapi_base";
                break;
            case "payFMGateway":
                redirect_uri = wechat.getBaseUrl() + "auth/payFMGateway" + params;// 微信固定金额二维码支付处理
                scope = "snsapi_base";
                break;
            case "payViewWebGateway":
                redirect_uri = wechat.getBaseUrl() + "auth/payViewWebGateway" + params;// 手机端支付处理
                scope = "snsapi_base";
                break;
            default:
                break;
        }
        //https://www.iknowtec.net/merchant/payGateway?payId=39&sign=a333db341ad6f7d7afad6c3534b981c4
        logger.info("redirect_uri==" + redirect_uri);
        return this.createPostUrl(redirect_uri, scope);
    }

    private String createPostUrl(String redirect_uri, String scope) {
        // TODO Auto-generated method stub
        String postUrl = null;
        try {
            postUrl = "https://open.weixin.qq.com/connect/oauth2/authorize?appid="
                    + wechat.getAppId()
                    + "&redirect_uri="
                    + URLEncoder.encode(redirect_uri, "utf-8")
                    + "&response_type=code&scope="
                    + scope
                    + "&state=1"
                    + "#wechat_redirect";
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return postUrl;
    }




    public String createAliPostUrl(String params) {
        String redirect_uri = wechat.getBaseUrl() + "auth/userAgent" + params;
        String postUrl = null;
        try {
            postUrl = "https://openauth.alipay.com/oauth2/publicAppAuthorize.htm?app_id="
                    + wechat.getAlipayappid()
                    + "&auth_skip=false&scope=auth_userinfo,auth_user,auth_base&redirect_uri="
                    + URLEncoder.encode(redirect_uri, "utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return postUrl;
    }

    public String createAliPostUrl(String action,String params) {
        String postUrl = null;
        String redirect_uri = wechat.getBaseUrl() + "auth/userAgent" + params;
        switch (action) {
            case "payGateway":
                redirect_uri = wechat.getBaseUrl() + "auth/payGateway" + params;// 第三方发起支付 微信支付进入
                break;
            case "payFMGateway":
                redirect_uri = wechat.getBaseUrl() + "auth/payFMGateway" + params;// 第三方发起支付 微信支付进入
                break;
            case "payViewWebGateway":
                redirect_uri = wechat.getBaseUrl() + "auth/payViewWebGateway" + params;// 手机端支付处理
                break;
            default:
                break;
        }
        try {
            postUrl = "https://openauth.alipay.com/oauth2/publicAppAuthorize.htm?app_id="
                    + wechat.getAlipayappid()
                    + "&auth_skip=false&scope=auth_userinfo,auth_user,auth_base&redirect_uri="
                    + URLEncoder.encode(redirect_uri, "utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return postUrl;
    }


}
